package com.liqi.device.service.impl;

import com.liqi.common.annotation.DataScope;
import com.liqi.common.utils.DateUtils;
import com.liqi.device.domain.DvGroup;
import com.liqi.device.mapper.DvGroupMapper;
import com.liqi.device.service.IDvGroupService;
import com.liqi.system.domain.SysRegion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分组Service业务层处理
 * 
 * @author Lwy
 * @date 2022-03-28
 */
@Service
public class DvGroupServiceImpl implements IDvGroupService {
    @Autowired
    private DvGroupMapper dvGroupMapper;

    /**
     * 查询分组
     *
     * @param id 分组主键
     * @return 分组
     */
    @Override
    public DvGroup selectDvGroupById(Long id) {
        return dvGroupMapper.selectDvGroupById(id);
    }

    /**
     * 查询分组列表
     *
     * @param dvGroup 分组
     * @return 分组
     */
    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<DvGroup> selectDvGroupList(DvGroup dvGroup) {
        return dvGroupMapper.selectDvGroupList(dvGroup);
    }

    /**
     * 通过上级id查询分组列表
     *
     * @param parentId 上级id
     * @return 分组集合
     */
    @Override
    public List<DvGroup> selectDvGroupByParentId(Long parentId) {
        return dvGroupMapper.selectDvGroupByParentId(parentId);
    }

    //查询多级分组信息
    @Override
    public Map findDvGroupTreeByPid(Long parentId){
        Map<Long, List<DvGroup>> map = new HashMap<>();
        List<DvGroup> list = selectDvGroupByParentId(parentId);
        Long id = parentId;
        map.put(id, list);
        //调用递归，查询所有下级
        //findRegionDigui(list, map);
        return map;
    }

    public void findRegionDigui(List<DvGroup> dvGroups, Map<Long, List<DvGroup>> map) {
        List<DvGroup> reList = new ArrayList<>();
        for (DvGroup r : dvGroups) {
            //通过ID查询下一级
            reList = selectDvGroupByParentId(r.getId());
            if (reList.size() > 0) {//代表有下级
                map.put(r.getId(), reList);
                findRegionDigui(reList, map); //循环调用自己
            }
        }
    }


    /**
     * 新增分组
     *
     * @param dvGroup 分组
     * @return 结果
     */
    @Override
    public int insertDvGroup(DvGroup dvGroup) {
        dvGroup.setCreateTime(DateUtils.getNowDate());
        return dvGroupMapper.insertDvGroup(dvGroup);
    }

    /**
     * 修改分组
     *
     * @param dvGroup 分组
     * @return 结果
     */
    @Override
    public int updateDvGroup(DvGroup dvGroup) {
        dvGroup.setUpdateTime(DateUtils.getNowDate());
        return dvGroupMapper.updateDvGroup(dvGroup);
    }

    /**
     * 批量删除分组
     *
     * @param ids 需要删除的分组主键
     * @return 结果
     */
    @Override
    public int deleteDvGroupByIds(Long[] ids) {
        return dvGroupMapper.deleteDvGroupByIds(ids);
    }

    /**
     * 删除分组信息
     *
     * @param id 分组主键
     * @return 结果
     */
    @Override
    public int deleteDvGroupById(Long id) {
        return dvGroupMapper.deleteDvGroupById(id);
    }

    @Override
    public int deleteDvGroupByAncestors(Long id) {
        return dvGroupMapper.deleteDvGroupByAncestors(id);
    }

    @Override
    public List<DvGroup> selectDvGroupListForDevice(DvGroup dvGroup) {
        return dvGroupMapper.selectDvGroupListForDevice(dvGroup);
    }

    @Override
    public List<Map<String, String>> getGrouplist(Long regionCode, Long parentCode) {
        return dvGroupMapper.getGrouplist(regionCode,parentCode);
    }


}



    
    


